package com.note.notes.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.note.common.domain.Folder;
import com.note.common.dto.FolderDto;
import com.note.common.dto.group.FolderGroup;
import com.note.common.enums.LogEnum;
import com.note.common.enums.ResponseEnum;
import com.note.common.interfaces.SystemLog;
import com.note.common.vo.ResponseVo;
import com.note.config.config.CustomBlockHandler;
import com.note.notes.service.FolderService;
import com.note.notes.service.impl.NoteServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Package：com.note.notes.controller
 * @Name：FolderController
 * @Author：热伊木
 * @Email：uyevan@163.com
 * @Date：2023-12-18-18:55
 * @Description：笔记文件夹目录控制类
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/folder")
@Tag(name = "NoteFolderController", description = "笔记文件夹目操作相关接口")
public class FolderController {

    @Autowired
    private FolderService folderService;
    @Autowired
    private NoteServiceImpl noteServiceImpl;

    /*文件夹创建|文件夹列表|文件夹删除*/

    @ResponseBody
    @PostMapping("/createFolder")
    @SystemLog(type = LogEnum.SAVE_LOG)
    @Operation(summary = "createFolder", description = "创建文件夹")
    public ResponseVo createFolder(@RequestBody @Validated({FolderGroup.class}) FolderDto folderDto) {
        folderService.createFolder(folderDto);
        return new ResponseVo(ResponseEnum.FOLDER_CREATE_SUCCESS);
    }

    @ResponseBody
    @GetMapping("/listFolders")
    @SystemLog(type = LogEnum.CHECK_LOG)
    @SentinelResource(
            value = "listFolders",
            blockHandlerClass = CustomBlockHandler.class,
            blockHandler = "blockHandleException",
            fallbackClass = CustomBlockHandler.class,
            fallback = "fallBackHandle"
    )
    @Operation(summary = "listFolders", description = "获取文件夹列表")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "parentId", description = "父文件夹Id", required = true, in = ParameterIn.QUERY)
    })
    public ResponseVo listFolders(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "父级目录ID不能为空") String parentId) {
        List<Folder> folders = folderService.listFolders(username, parentId);
        if (CollectionUtils.isEmpty(folders)) {
            return new ResponseVo(ResponseEnum.FOLDER_NULL, null);
        }
        return new ResponseVo(ResponseEnum.SUCCESS, folders);
    }

    @ResponseBody
    @DeleteMapping("/deleteFolder")
    @SystemLog(type = LogEnum.REMOVE_LOG)
    @Operation(summary = "deleteFolder", description = "删除笔记主文件夹")
    @Parameters({
            @Parameter(name = "username", description = "用户名", required = true, in = ParameterIn.QUERY),
            @Parameter(name = "folderId", description = "文件夹Id", required = true, in = ParameterIn.QUERY)
    })
    public ResponseVo deleteFolder(
            @RequestParam @NotBlank(message = "用户名不能为空") String username,
            @RequestParam @NotBlank(message = "文件夹Id不能为空") String folderId) {
        /**
         * 删除文件夹流程：
         * 1.通过文件夹ID遍历内部的文件夹和笔记
         * 2.其中的笔记直接进行删除、文件夹再次进行递归查询知道没有内层文件夹未知
         */
        folderService.deleteFolder(username, folderId);
        return new ResponseVo(ResponseEnum.DELETE_FOLDER_SUCCESS);
    }
}
